home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / SpinBarrier.cc < prev    next >
C/C++ Source or Header  |  1990-07-09  |  1KB  |  82 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. // 
  3. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  4. // Copyright (C) 1989 University of Colorado, Boulder, Colorado
  5. // Copyright (C) 1990 University of Colorado, Boulder, Colorado
  6. //
  7. // written by Dirk Grunwald (grunwald@foobar.colorado.edu)
  8. //
  9. #include <stream.h>
  10. #include "SpinBarrier.h"
  11. #include "CpuMultiplexor.h"
  12. #include "Pragma.h"
  13.  
  14. C_EXTERNAL_FUNCTION( int getpid() );
  15.  
  16. //
  17. //    Make everyone exit
  18. //
  19. #ifdef USE_SHARED_MEMORY
  20.  
  21. void
  22. SpinBarrier::lowerBarrier()
  23. {
  24.     reserve();
  25.     generation++;
  26.     pCount = 0;
  27.     release();
  28. }
  29.  
  30. int
  31. SpinBarrier::rendezvous()
  32. {
  33.     reserve();
  34.     pCount++;
  35.  
  36.     //
  37.     // Waiting people just read generation, and the final process
  38.     // just writes it, so we dont really even need to use the locks here.
  39.     //
  40.     if ( pCount == pHeight ) {
  41.     pCount = 0;
  42.     generation++;
  43.     release();
  44.     } else {
  45.  
  46.     short gen = generation;
  47.     VOLATILE short *genp = &generation;    // bug in 1.35
  48.  
  49.     release();
  50.     int timesAround = 0;
  51.     int totalGetPids = 0;
  52.     while ( gen == *genp ) {
  53.         if ( timesAround > pLoops ) {
  54.         (void) getpid();
  55.         timesAround = 0;
  56.         totalGetPids++;
  57.         if ( pMaxLoops > 0 && totalGetPids > pMaxLoops ) {
  58.             return(0);
  59.         }
  60.         }
  61.         timesAround++;
  62.  
  63.     }
  64.     }
  65.     return(1);
  66. }
  67. #endif /* USE_SHARED_MEMORY */
  68.  
  69. void
  70. SpinBarrier::height(int newHeight)
  71. {
  72.     reserve();
  73.     if (pCount >= newHeight) {
  74.     pCount = 0;
  75.     generation++;
  76.     }
  77.     else {
  78.     pHeight = newHeight;
  79.     }
  80.     release();
  81. }
  82.